import { baseUrl } from './config';

/**
 * request请求
 * @param {String} url 请求地址
 * @param {String} method 请求方式
 * @param {Object} data 发送参数
*/
function request(url, method, data){
	const client_info = uni.getStorageSync('client_info');
	const header = {
		'content-type': 'application/json',
		'token': client_info.token,
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method,
			data,
			header,
			timeout: 10000,
			success: (res) => {
                const result = res.data;
				switch (result.code) {
					case 1:
						break;
					case 0:
						uni.showToast({
							title: result.msg || '加载失败',
							icon: 'none',
							mask: true
						});
						console.log('request success code "500" ==>', url, result);
						break;
					case -1:
						uni.showToast({
							title: result.msg || '加载失败',
							icon: 'none',
							mask: true
						});
						setTimeout(() => {
							uni.reLaunch({
								url: `/subpkg/login/login`
							})
						},1000)
						break;
					default:
						uni.showToast({
							title: result.msg || '加载失败',
							icon: 'none'
						});
						console.log('request success code "defult" ==>', url, result);
						break;
				}
				resolve(result);
			},
			fail: (err_1) => {
				switch (err_1) {
					case 'request:fail timeout':
						uni.showToast({
							title: '网络超时,请稍后打开!',
							icon: 'error'
						});
						break;
					default:
						uni.showToast({
							title: '加载失败',
							icon: 'error'
						});
						console.log('request fail ==>', url, err_1);
						break;
				}
				reject(err_1);
			}
		});
	});
}

export default request